﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BarcodeDelay.view
{
    public partial class FrmJTDelaySetting : Form
    {
        public FrmJTDelaySetting()
        {
            InitializeComponent();
        }

        private void btnSaveSetting_Click(object sender, EventArgs e)
        {
            try
            {
                bool isShowTransferDelay = this.cbTransfer.Checked;
                bool isShowReturnDelay = this.cbReturn.Checked;
                bool isShowProblem = this.cbProblem.Checked;
                bool useOperation = StoParams.Instance.isUseOperationTime;
                JObject jsonObj = new JObject();
                jsonObj["isShowTransferDelay"] = isShowTransferDelay;
                jsonObj["isShowReturnDelay"] = isShowReturnDelay;
                jsonObj["isShowProblem"] = isShowProblem;
                jsonObj["useOperation"] = useOperation;

                StoParams.Instance.isShowTransfer = isShowTransferDelay;
                StoParams.Instance.isShowReturn = isShowReturnDelay;
                StoParams.Instance.isShowProblem = isShowProblem;

                FileStream bFile = new FileStream(StoParams.Instance.SettingPath, FileMode.Create);
                StreamWriter swb = new StreamWriter(bFile);
                swb.Write(jsonObj.ToString());
                swb.Close();
                swb = null;
                MessageBox.Show("延误配置保存成功");


            }
            catch(Exception ex)
            {
                MessageBox.Show("延误配置保存失败");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("关闭之前请确认已保存过相关配置", "是否已经保存", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }
            this.Close();
        }

        private void rbAdd1Hour_CheckedChanged(object sender, EventArgs e)
        {
            if(rbAdd1Hour.Checked)
            {
                StoParams.Instance.isUseOperationTime = false;
            }
            else
            {
                StoParams.Instance.isUseOperationTime = true;
            }
        }

        private void rbByOperation_CheckedChanged(object sender, EventArgs e)
        {
            if (rbByOperation.Checked)
            {
                StoParams.Instance.isUseOperationTime = true;
            }
            else
            {
                StoParams.Instance.isUseOperationTime = false;
            }
        }

        private void FrmJTDelaySetting_Load(object sender, EventArgs e)
        {
            this.cbTransfer.Checked = StoParams.Instance.isShowTransfer;
            this.cbReturn.Checked = StoParams.Instance.isShowReturn;
            this.cbProblem.Checked = StoParams.Instance.isShowProblem;
            if(StoParams.Instance.isUseOperationTime)
            {
                this.rbByOperation.Checked = true;
            }
            else
            {
                this.rbAdd1Hour.Checked = true;
            }
        }
    }
}
